package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.GoodsDTO;
import com.nit.lab.dto.basic.GoodsListDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.GoodsListService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/api/goodsList")
public class GoodsListController {

    @Resource
    private GoodsListService goodsListService;

    private final Logger logger = LoggerFactory.getLogger(GoodsListController.class);
    /**
     * @description: 添加商品
     * @Author: WJM
     * @Date: 2022/7/16 16:20
     **/
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add( @RequestBody GoodsListDTO goodsListDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsListService.add(goodsListDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录主表记录
     * @Author: ZB
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String ids = jsonRequest.getString("listIds");
            opResult.setMessage(goodsListService.remove1(ids).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新主表记录
     * @Author: ZB
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody GoodsListDTO goodsListDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsListService.update(goodsListDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取主表数据
     * @Author: ZB
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = goodsListService.list4Table(tableReqDTO);
            List<GoodsDTO> records = tableRspDTO.getResult().getData();
//            System.out.println(records.get(0));
//            System.out.println(records);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }


    /**
     * @Description:  上链后将goods_status由0改为1
     * @Author: skw
     * @Date: 2022/06/28
     */
    @RequestMapping(value = "/editStatus", method = RequestMethod.POST)
    public OpResultDTO editStatus(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        System.out.println("标记点");
        Integer listId = (Integer) jsonObject.get("listId");
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsListService.editStatus(listId).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @description:
     * @Author: WJM
     * @Date: 2022/7/17 15:33
     **/
    @RequestMapping(value = "/getGoods", method = RequestMethod.POST)
    public OpResultDTO getGoods(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String listIds = jsonRequest.getString("listIds");
            opResult.setResult(goodsListService.getGoods(listIds));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


//    提供给小程序的
    @RequestMapping(value = "/querylist2", method = RequestMethod.POST)
    public OpResultDTO listGoodsData2(){
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setResult(goodsListService.listGoodsData());
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return opResult;
    }

    @RequestMapping(value = "/querylist", method = RequestMethod.GET)
    public JSON listGoodsData() {
        JSON json = null;
        try {
            List<GoodsListDTO> list1 = goodsListService.listGoodsData();
            json = (JSON)JSON.toJSON(list1);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return json;
    }
    /**
     * @Description: 通过商品ID获取该商品信息
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/queryget2", method = RequestMethod.POST)
    public OpResultDTO getGoodsInfo2(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String listId = jsonRequest.getString("listId");
            opResult.setResult(goodsListService.getGoodsInfo(listId));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return opResult;
    }
    @RequestMapping(value = "/queryget", method = RequestMethod.GET)
    public JSON getGoodsInfo(String listId) {
        JSON json = null;
        try {
            List<GoodsListDTO> list1 = goodsListService.getGoodsInfo(listId);
            json = (JSON)JSON.toJSON(list1);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return json;
    }

    @RequestMapping(value = "/buy", method = RequestMethod.POST)
    public OpResultDTO Buy(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            Integer id = jsonRequest.getInteger("id");
            opResult.setResult(goodsListService.buy(id));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/bought", method = RequestMethod.POST)
    public OpResultDTO bought(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String status = jsonRequest.getString("status");
            Integer id = jsonRequest.getInteger("id");
            String owner = jsonRequest.getString("owner");
            opResult.setResult(goodsListService.bought(status, id, owner));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 通过商品ID获取该商品信息
     * @Author: ZB
     */
    @RequestMapping(value = "/querygetgoods", method = RequestMethod.GET)
    public JSON getBasicGoodsInfo(String hashId) {
        JSON json = null;
        try {
            List<GoodsDTO> list1 = goodsListService.getBasicGoodsInfo(hashId);
            json = (JSON)JSON.toJSON(list1);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return json;
    }
//    temp 可以去掉
    @RequestMapping(value = "/getAllGoods", method = RequestMethod.POST)
    public OpResultDTO GetAllGoods(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            Integer goodsId = jsonRequest.getInteger("goodsId");
            opResult.setResult(goodsListService.getAllGoods(goodsId));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
